export default {
    SET_BANNAR (state, data) {
        state.bannar = data
    },
    SET_CLASSIFYS (state, data) {
        state.classifys = data
    },
    SAVE_USERINFO (state, data) {
        state.userInfo = data
    },
    CHANGE_CITY (state, data) {
    state.selectCity = data
    },
    CHAGNE_SITE (state, name) {
    state.selectSite = name
    },
    CHANGE_LOCATION (state, data) {
    state.location = data
    },
    SAVE_SITE (state, data) {
    state.sites.push(data)
    },
    SAVE_SITES (state, data) {
    state.sites = data
    },
    CHANGE_SEHLECTED_SITE (state, data) {
    state.userInfo.selectedSite = data
    },
    // 更改地址列表中的地址
  CHANG_SITES (state, site) {
    for (var i = 0; i < state.sites.length; i++) {
      if (state.sites[i].id === Number(site.id)) {
        state.sites[i] = site
        break
      }
    }
  },
  SAVE_CARTS (state, data) {
    state.carts = data
  },
  ADD_CART (state, data) {
    state.carts.push(data)
  },
  DEL_CART (state, data) {
    for (let i = 0; i < state.carts.length; i++) {
      if (state.carts[i].id === data.id) {
        for (let z = 0; z < state.classifys.length; z++) {
          for (let j = 0; j < state.classifys[z].products.length; j++) {
            if (state.classifys[z].products[j].unit === state.carts[i].unit) {
              // console.log(state.classifys[z].products[j])
              state.classifys[z].products[j].num--
              break
            }
          }
        }
        state.carts.splice(i, 1)
        break
      }
    }
  },
  SUB_CART (state, data) {
    for (let i = 0; i < state.carts.length; i++) {
      if (state.carts[i].id === data.id) {
        //闪送超市商品数量随着购物车数量改变而改变
        for (let z = 0; z < state.classifys.length; z++) {
          for (let j = 0; j < state.classifys[z].products.length; j++) {
            if (state.classifys[z].products[j].unit === state.carts[i].unit) {
              // console.log(state.classifys[z].products[j])
              state.classifys[z].products[j].num--
              break
            }
          }
        }
        state.carts[i].num--
        break
      }
    }
  },
  UPDATE_CART (state, data) {
    for (let i = 0; i < state.carts.length; i++) {
      if (state.carts[i].id === data.id) {
        //闪送超市商品数量随着购物车数量改变而改变
        for (let z = 0; z < state.classifys.length; z++) {
          for (let j = 0; j < state.classifys[z].products.length; j++) {
            if (state.classifys[z].products[j].unit === state.carts[i].unit) {
              // console.log(state.classifys[z].products[j])
              state.classifys[z].products[j].num += 1
              break
            }
          }
        }
        state.carts[i].num++
        break
      }
    }
  },
  // 初始化原始分类数据
  UPDATE_CLASSIFYS (state, carts) {
    let classifys = state.classifys
    for (let i = 0; i < carts.length; i++) {
      for (let j = 0; j < classifys.length; j++) {
        let products = classifys[j].products
        for (let z = 0; z < products.length; z++) {
          if (Number(carts[i].product_id) === Number(products[z].product_id)) {
            products[z].num = carts[i].num
          }
        }
      }
    }
  },
  CART_POS (state, cartPos) {
    state.cartPos = cartPos
  },
  CHANGE_CARTACTIVE_TRUE (state) {
    state.cartActive = true
  },
  CHANGE_CARTACTIVE_FALSE (state) {
    state.cartActive = false
  },
  SWITCH_SELECT (state, item) {
    let index = state.carts.indexOf(item)
    state.carts[index].selected = !state.carts[index].selected
  },
  CHANGE_ALL_SELECTED (state) {
    for (let i = 0; i < state.carts.length; i++) {
      state.carts[i].selected = !state.carts[i].selected
    }
  }
}